package com.biglead.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.demo.common.PageRequest;
import com.biglead.demo.common.PageResult;
import com.biglead.demo.common.PageUtils;
import com.biglead.demo.mapper.UserMapper;
import com.biglead.demo.pojo.UserCategory;
import com.biglead.demo.pojo.UserInfo;
import com.biglead.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserInfo> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List<UserInfo> findList() {
        return userMapper.selectList();
    }

    @Override
    public UserInfo newUser(UserInfo info) {
        //保存用户
        this.save(info);
        return info;
    }
    @Override
    public UserInfo updateUser(UserInfo info) {
        //根据ID来更新用户信息
        this.updateById(info);
        return info;
    }
    @Override
    public UserInfo delete(UserInfo info) {
        //根据ID来删除用户信息
        this.delete(info);
        return info;
    }
    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    @Override
    public UserInfo getByUsername(String username) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件 ID = 1 的数据
        queryWrapper.eq(UserInfo::getUserName,username);
        //查询用户
        UserInfo userInfo = this.getOne(queryWrapper);
        return userInfo;
    }

    /**
     * 调用分页插件完成分页
     * @return
     */
    private PageInfo<UserInfo> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<UserInfo> userInfoList = userMapper.selectPage();
        return new PageInfo<UserInfo>(userInfoList);
    }

}
